import 'package:flutter/widgets.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:demo/event/event_setting.dart';
import 'package:demo/views/base/base_controller.dart';
import 'package:demo/tools/tools_enum.dart';
import 'package:demo/tools/tools_sqlite.dart';

class GroupDetailsController extends BaseController {
  TextEditingController nameController = TextEditingController();
  TextEditingController noticeController = TextEditingController();
  ChatGroup chatGroup = ChatGroup.init();
  late String groupId;

  // 获取详情
  getById() async {
    chatGroup = await ToolsSqlite().group.getById(groupId);
    // 更新
    _update();
  }

  // 更新
  _update() {
    nameController.text = chatGroup.groupName;
    noticeController.text = chatGroup.notice;
    update();
  }

  // 修改头像
  editPortrait(String portrait) async {
    if (portrait.isEmpty) {
      return;
    }
    chatGroup.portrait = portrait;
    update();
    // 通知
    EventSetting().handle(
      SettingModel(
        SettingType.group,
        primary: groupId,
        label: 'portrait',
        value: portrait,
      ),
    );
    // 提醒
    EasyLoading.showToast('修改成功');
  }

  // 修改群名
  editGroupName() async {
    String groupName = nameController.text.trim();
    // 更新
    chatGroup.groupName = groupName;
    update();
    // 通知
    EventSetting().handle(
      SettingModel(
        SettingType.group,
        primary: groupId,
        label: 'groupName',
        value: groupName,
      ),
    );
    // 提醒
    EasyLoading.showToast('修改成功');
    // 返回
    Get.back();
  }

  // 修改公告
  editNotice() async {
    String notice = noticeController.text.trim();
    // 更新
    chatGroup.notice = notice;
    update();
    // 通知
    EventSetting().handle(
      SettingModel(
        SettingType.group,
        primary: groupId,
        label: 'notice',
        value: notice,
      ),
    );
    // 提醒
    EasyLoading.showToast('修改成功');
    // 返回
    Get.back();
  }

  // 置顶
  setTop(String top) {
    // 更新
    chatGroup.memberTop = top;
    update();
    // 通知
    EventSetting().handle(
      SettingModel(
        SettingType.group,
        primary: groupId,
        label: 'memberTop',
        value: top,
      ),
    );
  }

  // 静默
  setDisturb(String disturb) {
    // 更新
    chatGroup.memberDisturb = disturb;
    update();
    // 通知
    EventSetting().handle(
      SettingModel(
        SettingType.group,
        primary: groupId,
        label: 'memberDisturb',
        value: disturb,
      ),
    );
  }

  // 解散
  logout() {
    // 通知
    EventSetting().handle(
      SettingModel(
        SettingType.group,
        primary: groupId,
        label: 'delete',
      ),
    );
    // 返回上一页
    Get.back();
    // 提醒
    EasyLoading.showToast('退出成功');
    // 返回上一页
    Get.back();
  }

  // 清空消息
  clearHis() {
    // 通知
    EventSetting().handle(
      SettingModel(
        SettingType.clear,
        primary: groupId,
      ),
    );
    // 提醒
    EasyLoading.showToast('清空成功');
  }

  @override
  void onInit() {
    super.onInit();
    groupId = Get.arguments;
    getById();
    // 监听群聊
    subscription = EventSetting().event.stream.listen((model) {
      if (SettingType.group == model.setting) {
        getById();
      }
    });
  }

  @override
  void onClose() {
    nameController.dispose();
    noticeController.dispose();
    super.onClose();
  }
}
